package com.drug.controller;

import com.drug.domain.*;
import com.drug.service.StatisticsService;
import com.drug.util.DateUtils;
import com.drug.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/StatisticsController")
public class StatisticsController {
    @Autowired
    private StatisticsService statisticsService;

    /**
     * 根据订单查询
     * @param statistics1
     * @return
     */
    @RequestMapping("/StatisticsFindAllPd")
    private Result statisticsFindAllPd(Statistics1 statistics1){
       return statisticsService.statisticsFindAllPd(statistics1);
    }
    /**
     * 根据供货商查询
     * @param statistics2
     * @return
     */
    @RequestMapping("/StatisticsFindAllSp")
    private Result statisticsFindAllSp(Statistics2 statistics2){
        return statisticsService.statisticsFindAllSp(statistics2);
    }

    /**
     * 根据医院名称查询
     *
     */
    @RequestMapping("/StatisticsFindAllHs")
    private Result StatisticsFindAllHs(Statistics3 statistics3){
        return statisticsService.statisticsFindAllHs(statistics3);
    }

    /**
     * 根据交易药品查询 详细信息
     * @param statistics1
     * @return
     */
    @RequestMapping("/StatisticsFindAllDi")
    private Result statisticsFindAllDi(Statistics1 statistics1){
        return statisticsService.statisticsFindAllPd(statistics1);
    }

    /**
     * 导出退货单
     *
     */
    @RequestMapping(value = "/findAllExport")
    public String export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String message = "";
        //获取数据
        List<PurchaseOrder> list = statisticsService.findAllExport();
        //excel标题
        String[] title = {"采购医院","采购单编号","采购单名称","采购状态","开始采购时间","结束采购时间","建单时间","提交时间","审核时间"};
        //excel文件名
        String fileName = "Settlement"+System.currentTimeMillis()+".xls";
        //sheet名
        String sheetName = "采购单信息表";
        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            PurchaseOrder purchaseOrder = list.get(i);
            Hospital hospital = purchaseOrder.getHospital();
            String gethName =null;
            if (hospital!=null){
                 gethName = hospital.gethName();
            }
            content[i][0]=gethName+"";
            String state1=null;
            PodiPd podiPd = purchaseOrder.getPodiPd();
            if (podiPd!=null){
              String  state = podiPd.getPodiState();
                if (state.equals("0")) {
                    state1 ="已发货";
                }else if (state.equals("1")) {
                    state1 ="未发货";
                }else if (state.equals("2")) {
                    state1 ="已入库";
                }
            }
            content[i][3]=state1+"";
            String poNumber = purchaseOrder.getPoNumber();
            content[i][1]=poNumber+"";
            String poName = purchaseOrder.getPoName();
            content[i][2]=poName+"";
            Date poStateTime = purchaseOrder.getPoStateTime();
            String s1 = DateUtils.date2String(poStateTime, "yyyy-MM-dd");
            content[i][4]=s1+"";
            Date poEndTime = purchaseOrder.getPoEndTime();
            String s2 = DateUtils.date2String(poEndTime, "yyyy-MM-dd");
            content[i][5]=s2+"";
            Date poCreateTime = purchaseOrder.getPoCreateTime();
            String s3 = DateUtils.date2String(poCreateTime, "yyyy-MM-dd");
            content[i][6]=s3+"";
            Date poSubmissionTime = purchaseOrder.getPoSubmissionTime();
            String s4 = DateUtils.date2String(poSubmissionTime, "yyyy-MM-dd");
            content[i][7]=s4+"";
            Date poAuditTime = purchaseOrder.getPoAuditTime();
            String s5 = DateUtils.date2String(poAuditTime, "yyyy-MM-dd");
            content[i][8]=s5+"";
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }
    /**
     * 导出供货商查询
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/findAllExport1")
    public String export1(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String message = "";
        //获取数据
        List<Supplier> list= statisticsService.findAllExport1();

        //excel标题
        String[] title = {"供货商","采购状态","订货量","订货金额","送货量","送货金额","退货量","退货金额","结算量","结算金额"};
        //excel文件名
        String fileName = "supplier"+System.currentTimeMillis()+".xls";
        //sheet名
        String sheetName = "供货商单信息表";
        String [][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            Supplier supplier = list.get(i);
            String surName = supplier.getSurName();
            content[i][0]=surName+"";
            List<PodiPd> podiPd = supplier.getPodiPd();
            for (PodiPd pd : podiPd) {
                String podiState = pd.getPodiState();
                content[i][1]=podiState+"";
                String podiOrder = pd.getPodiOrder();
                content[i][2]=podiOrder+"";
                String podiOrderTotal = pd.getPodiOrderTotal();
                content[i][3]=podiOrderTotal+"";
                String podiDelivery = pd.getPodiDelivery();
                content[i][4]=podiDelivery+"";
                String podiDeliveryTotal = pd.getPodiDeliveryTotal();
                content[i][5]=podiDeliveryTotal+"";
                String podiReturn = pd.getPodiReturn();
                content[i][6]=podiReturn+"";
                String podiReturnTotal = pd.getPodiReturnTotal();
                content[i][7]=podiReturnTotal+"";
                String podiSettlement = pd.getPodiSettlement();
                content[i][8]=podiSettlement+"";
                String podiSettlementTotal = pd.getPodiSettlementTotal();
                content[i][9]=podiSettlementTotal+"";
            }
        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            message = "SUCCESS";
        } catch (Exception e) {
            e.printStackTrace();
            message = "ERROR";
        }
        return null;
    }
    /**
     * 查询供货商的药品信息
     * @param hId
     * @return
     */
    @RequestMapping("/findOneDiByHs")

    private Result findOneDiByHs(Integer hId){
        return statisticsService.findOneDiByHs(hId);
    }
    /**
     * 查询供货商的药品信息
     * @param surId
     * @return
     */
    @RequestMapping("/findOneDiBySp")
    private Result findOneDiBySp(Integer surId){
        return statisticsService.findOneDiBySp(surId);
    }

    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
